<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/17
 * Time: 11:52
 */

namespace SDXApi\Controller;


use SDXApi\Service\OssUploadService;
use SDXApi\Service\SeminarService;
use SDXApi\Service\SeminarsService;
use Think\Controller;

/**
 * Class SeminarController
 * @package SDXApi\Controller
 */
class SeminarController extends AppController
{
    public function _initialize($apptoken, $apptime)
    {
        parent::_initialize($apptoken, $apptime, false, '',true,false);
    }

	/**
	 * 返回数据
	 * Created by Jerry
	 */
	public function get_seminar_template_page()
	{
		$user_id = I('post.uid', 0, 'int');
		$id = I('post.seminar_id', 0, 'int');
		$wid = I('post.wid', 8, 'int');
		$is_show = I('post.is_show', 0, 'int');
		$is_app = I('post.is_app', 0, 'int');
		$version = I('post.version', 0, 'int');
        $pageNumber = I('post.page', 0, 'int');

		if (empty($id) || empty($wid)) {
			returnApiError('参数缺失');
		}

//		$data = array();
//
//		$userRank = $this->getUserRank($user_id);
//
//
//		$data = S('seminar_template_page_data_' . $id . '_' . $userRank . '_is_app_' . $is_app . '_version_' . $version);
//
//		if (empty($data) || $is_show == 1) {
//			$seminarService = new SeminarService($user_id, $wid);
//
//			$seminarService->is_show = $is_show;
//			$seminarService->is_app = $is_app;
//			$seminarService->version = $version;
//
//			$template = $seminarService->getTemplateById($id);
//
//			if (empty($template)) {
//				returnApiError('暂无数据');
//			}
//
//			$data['seminar_name'] = $template['name'];
//			$data['desc'] = $template['desc'];
//			$data['seminar_list'] = $seminarService->findSeminarTemplatePageById($id);
//
//			S('seminar_template_page_data_' . $id . '_' . $userRank, $data, mt_rand(3600, 4200));
//
//		}

//		if (empty($data)) {
//			returnApiError('暂无数据');
//		}

		$seminarService = new SeminarsService();
		$seminarService->is_show = $is_show;
		$seminarService->userId = $user_id;
		$seminarService->wid = $wid;

        $pageSize = D('seminarPage')->find_seminar_template_pages_count_by_template_id($id);

		$seminarData = $seminarService->findSeminarTemplatePageById($id, $pageNumber, $pageSize);

		$template = $seminarService->getTemplateById($id);

//		$phone = $this->getUserPhoneBind($user_id);
//
//		if (empty($phone)) {
//            $data['bind_phone'] = 0;
//        }
//        else{
//            $data['bind_phone'] = 1;
//        }

		$data['seminar_name'] = $template['name'];
		$data['desc'] = $template['desc'];
        $data['background_color'] = $template['background_color'];
		$data['seminar_list'] = $seminarData;

		if($pageNumber) {
            $data['page'] = show_page($pageSize, $pageNumber, 5);

            $isLast = 0;
            if ($pageNumber >= $data['page']['pagecount']) {
                $isLast = 1;
            }

            $data['is_bottom'] = $isLast;
        }

		if (empty($template)) {
			returnApiError('专题过期');
		}

		/*
		 * 处理专题商品曝光行为
		 * Created By caicm
		 */
		if($user_id){
            foreach($data['seminar_list'] as $value){
                if($value['type']==3 && $value['goods_list']){
                    $param = array('isertList', $user_id, 'view', $value['goods_list'], $wid, 1,$data['seminar_name'], $id);
                    \Think\Hook::listen('receng', $param);
                }
            }
        }


		returnApiSuccess($data);
	}

    /**
     * Created by Jerry
     * @param int $userId
     * @return bool
     */
    protected function getUserPhoneBind($userId = 0)
    {
        $phone = D('Users')->findUserPhone($userId);

        if (empty($phone)) {
            return false;
        }

        return true;
    }

	/**
	 * Created by Jerry
	 * @param $userId
	 * @return int
	 */
	protected function getUserRank($userId)
	{

		$rank = D('Users')->get_user_rank($userId);

		if (empty($rank)) {
			return -1;
		}

		return $rank['user_rank'];
	}

//	public function testSeminarGoodsByGoodsId()
//	{
//		$goods_id = I('post.goods_id', 0, 'int');
//
//		$seminarService = new SeminarService();
//		$seminarList = $seminarService->getSeminarGoodsListByGoodsId($goods_id);
//		dd($seminarList);
//	}

	/**
	 * 缓存所有专题
	 * Created by Jerry
	 */
	public function cacheSeminar()
	{
		$clear = I('get.clear', 0, 'int');

		$ip = get_client_ip();

		if ($clear != 1) {
			return false;
		}

		$seminarService = new SeminarService();
		$seminarService->seminarTemplateCache();
	}

	public function testSeminar()
	{
		$user_id = I('post.uid', 0, 'int');
		$id = I('post.seminar_id', 0, 'int');
		$wid = I('post.wid', 0, 'int');
		$is_show = I('post.is_show', 0, 'int');


		$seminarService = new SeminarsService();
		$seminarService->is_show = $is_show;
		$seminarService->userId = $user_id;
		$seminarService->wid = $wid;

		$seminarData = $seminarService->findSeminarTemplatePageById($id);
		$template = $seminarService->getTemplateById($id);

		if (! $template) {
			returnApiError('暂无数据');
		}

		$data['seminar_name'] = $template['name'];
		$data['desc'] = $template['desc'];
		$data['seminar_list'] = $seminarData;

		returnApiSuccess($data);
	}

    /**
     * 签名上传用例
     * @author Jerry
     */
	protected function test_oss_upload_token()
    {
        $ossService = new OssUploadService();
        $token = $ossService->getToken('/upload/123', '123');
        returnApiSuccess($token);
    }
}